package contacts;

import gui.CheckBoxList;
import gui.JCheckBoxWithObject;
import gui.Test;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import tools.OptionReader;

public class VCardImportWindow extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4973430558059992975L;
	private JPanel contentPane;
	private JTextField textField;
	private Connection connection;
	private Statement stat;
	private LinkedList<CheckboxBook> llbooks = new LinkedList<CheckboxBook>();
	private DefaultListModel dlm;
	private CheckBoxList books;
	private File[] file;
	String[] durchsuchen = { "Durchsuchen...", "Browse...", "Scrutamini..." };
	String[] abbrechenS = { "Abbrechen", "Cancel", "Abrigavi" };
	String[] importieren = { "Importieren", "Import", "Importo" };
	String[] keineAuswahl = { "keine Auswahl", "no Selection",
			"non Selectionem" };
	String[] selectWindowName = { "iCal Datei auswählen", "Select iCal File",
			"Lego iCal File" };
	private int speak = 0;
	private int r;
	private int g;
	private int b;
	private OptionReader or;
	private String[] windowColorsSeperated;
	private VCardImportWindow vciw = this;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					VCardImportWindow frame = new VCardImportWindow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public VCardImportWindow() {

		or = new OptionReader();
		try {
			or.getOpionsFromFile();
		} catch (IOException e2) {

			e2.printStackTrace();
		}
		String windowColors = or.getColorSetting();
		windowColorsSeperated = windowColors.split(",");
		r = Integer.parseInt(windowColorsSeperated[0]);
		g = Integer.parseInt(windowColorsSeperated[1]);
		b = Integer.parseInt(windowColorsSeperated[2]);

		speak = Integer.parseInt(or.getLanguage());

		setMaximumSize(new Dimension(200, 2147483647));
		setTitle(".vcf Import");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 400, 347);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(r, g, b));
		contentPane.setMaximumSize(new Dimension(200, 32767));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel label = new JLabel("Die zu importierende .vcf Datei auswählen:");
		label.setBounds(5, 5, 440, 16);
		contentPane.add(label);

		textField = new JTextField();
		textField.setText("keine Auswahl");
		textField.setEditable(false);
		textField.setColumns(10);
		textField.setBounds(5, 43, 194, 20);
		contentPane.add(textField);

		JButton button = new JButton(durchsuchen[speak]);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				textField.setBackground(contentPane.getBackground());

				// file = FileDialog.getFiles(selectWindowName[speak]);
				file = new Importer().getFiles();
				if (file != null) {
					String str = "";
					for (int i = 0; i < file.length; i++) {
						if (i < file.length - 1) {
							str = str + file[i].getName() + ",";
						} else {
							str = str + file[i].getName();

						}
					}

					textField.setText(str);
				}
			}
		});
		button.setBounds(212, 40, 134, 23);
		contentPane.add(button);

		JLabel lblInWelchesAdressbuch = new JLabel(
				"In welches Adressbuch soll importiert werden?");
		lblInWelchesAdressbuch.setBounds(5, 97, 381, 14);
		contentPane.add(lblInWelchesAdressbuch);

		JLabel lblMeineAdressbcher = new JLabel("Meine Adressb\u00FCcher");
		lblMeineAdressbcher.setBounds(5, 135, 134, 14);
		contentPane.add(lblMeineAdressbcher);

		JButton button_2 = new JButton("Abbrechen");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		button_2.setBounds(95, 283, 121, 23);
		contentPane.add(button_2);

		JButton button_3 = new JButton("Importieren");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (textField.getText().equals(keineAuswahl[speak])) {
					System.out.println("isso");
					textField.setBackground(Color.RED);
				} else {

					Adressbuch ab = new Adressbuch("temp");
					ab.addPerson(file, vciw);
					Test mainInstance =Test.getMainInstance();
					mainInstance.getKontakte().update();
					dispose();

				}

			}
		});
		button_3.setBounds(226, 283, 120, 23);
		contentPane.add(button_3);

		dlm = new DefaultListModel();
		books = new CheckBoxList();
		books.setModel(dlm);

		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager
					.getConnection("jdbc:mysql://win9142.informatik.uni-wuerzburg.de:3306?user=awesomefireballs&password=jesus");
		} catch (ClassNotFoundException e) {

			e.printStackTrace();
		} catch (SQLException e) {

			e.printStackTrace();

		}
		try {
			stat = connection.createStatement();
			stat.executeQuery("use `sopra-ws12-awesome_fireballs`;");

			ResultSet idsBooks = stat
					.executeQuery("SELECT idBooks, name, idCreator FROM Books WHERE idUser = "
							+ Test.iduser + ";");
			boolean atleastone = false;
			while (idsBooks.next()) {

				CheckboxBook temp = new CheckboxBook(
						idsBooks.getInt("idBooks"), idsBooks.getString("name"),
						idsBooks.getInt("idCreator"));

				// Fuegt alle Personen interner Liste zu um spaeter wieder auf
				// id
				// zurueckgreifen zu koennen.
				llbooks.add(temp);

				JCheckBoxWithObject check = new JCheckBoxWithObject(temp);
				if (atleastone == false) {
					check.setSelected(true);
					atleastone = true;
				}
				dlm.addElement(check);
			}

		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		JScrollPane bookscroller = new JScrollPane(books);
		bookscroller.setBounds(5, 172, 344, 28 * 3);
		// bookscroller.setPreferredSize(new Dimension(280, 570));
		contentPane.add(bookscroller);

	}

	public LinkedList<Integer> getSelectedBooks() {
		LinkedList<Integer> llbooks = new LinkedList<Integer>();
		for (int i = 0; i < dlm.size(); i++) {
			JCheckBoxWithObject temp = (JCheckBoxWithObject) dlm.get(i);
			if (temp.isSelected() == true) {
				CheckboxBook temp2 = (CheckboxBook) temp.getObject();
				llbooks.add(temp2.getIdBooks());
			}

		}

		return llbooks;
	}
}
